package com.xbongbong.pro.statistic.abstracts;

import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.ExportFormDataVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.statistic.help.ChartExportHelp;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartPenetrateResultDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * @author zyy
 * @date 2022/11/24/ 14:11:00
 * @since v1.0
 */
public abstract class AbstractChartExport {

    @Resource
    private ChartExportHelp chartExportHelp;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    public Logger logger = LoggerFactory.getLogger(AbstractChartExport.class);

    public ExportFormDataVO exportChartData(ChartPenetrateResultDTO chartResultDTO) throws XbbException{
        ExportFormDataVO exportFormDataVO = new ExportFormDataVO();
        try {
            FormDataListDTO formDataListDTO = new FormDataListDTO();

            //处理导出状态
            chartExportHelp.initChartExport(formDataListDTO, chartResultDTO.getUserId(), chartResultDTO.getCorpid());

            //初始化导出的配置
            initExportConfig(chartResultDTO);

            //获取数据
            chartResultDTO.setExportFlag(true);
            ChartResultVO chartResultVO = getBaseExportData(chartResultDTO);

            //组装数据格式并上传到oss
            chartExportHelp.exPortData(exportFormDataVO, formDataListDTO, chartResultVO);
        }catch (XbbException e){
            String hashKey = chartResultDTO.getCorpid() + "_" + chartResultDTO.getUserId();
            paasRedisHelper.removeValue(RedisPrefixConstant.EXPORT_STATUS_KEY, hashKey);
            logger.error("导出失败", e);
        }
        return exportFormDataVO;
    }

    /**
     * 初始化导出配置
     *
     * @param chartResultDTO
     * @author zyy
     * @date 2022/11/24
     * @version v1.0
     */
    public abstract void initExportConfig(ChartPenetrateResultDTO chartResultDTO) throws XbbException;

    /**
     * 获取导出的基础数据
     *
     * @param chartResultDTO 入参
     * @author zyy
     * @date 2022/11/24
     * @version v1.0
     */
    public abstract ChartResultVO getBaseExportData(ChartPenetrateResultDTO chartResultDTO) throws XbbException;
}
